### R Script
### QCA

# Elina Sannehag & Felix Ettensperger

### Calibration

setwd("C:/Users/fe300/Desktop/Research Article - Reg Rel QCA")
Data_QCA_raw <- read.csv("Data_QCA_raw.csv")


library(QCA)
library(SetMethods)
library(tidyverse)

Data_QCA_full <- Data_QCA_raw

## Conditions QCA 1

Data_QCA_full$qca.relfrag <- calibrate(Data_QCA_full$al_religion2000, type = "fuzzy", method = "direct", c(0.05, 0.5, 0.75), logistic = TRUE)
Data_QCA_full$qca.relfrag <- round(Data_QCA_full$qca.relfrag, 3)

Data_QCA_full$qca.gdp <- calibrate(Data_QCA_full$mad_gdppc, type = "fuzzy", method = "direct", c(1000, 12500, 50000), logistic = TRUE)
Data_QCA_full$qca.gdp <- round(Data_QCA_full$qca.gdp, 3)

Data_QCA_full$qca.srel1900 <- Data_QCA_full$X1900
Data_QCA_full$qca.srel1900 <- round(Data_QCA_full$X1900, 3)  

Data_QCA_full$qca.fog <- calibrate(Data_QCA_full$fh_fog, type = "fuzzy", method = "direct", c(1, 6.5, 11), logistic = TRUE)
Data_QCA_full$qca.fog <- round(Data_QCA_full$qca.fog, 3)

Data_QCA_full$qca.supp.relgrp <- calibrate(Data_QCA_full$vdem_20ya_regsup_g7, type = "fuzzy", method = "direct", c(0.05, 0.10, 0.46), logistic = TRUE)
Data_QCA_full$qca.supp.relgrp <- round(Data_QCA_full$qca.supp.relgrp, 3)

Data_QCA_full$qca.supp.urbanwk <- calibrate(Data_QCA_full$vdem_20ya_regsup_g9, type = "fuzzy", method = "direct", c(0.05, 0.10, 0.46), logistic = TRUE)
Data_QCA_full$qca.supp.urbanwk <- round(Data_QCA_full$qca.supp.urbanwk, 3)

### Conditions QCA 2

Data_QCA_full -> Data_QCA_full_v2

Data_QCA_full_v2$qca.pref1980 <- calibrate(Data_QCA_full_v2$Preferred_Recoded, type = "fuzzy", method = "direct", c(0.5, 2.05, 3.5), logistic = TRUE)
Data_QCA_full_v2$qca.pref1980 <- round(Data_QCA_full_v2$qca.pref1980, 3)

Data_QCA_full_v2$qca.highcourt <- calibrate(Data_QCA_full_v2$vdem_20ya_juhcind, type = "fuzzy", method = "direct", c(-2, 0.5, 2), logistic = TRUE)
Data_QCA_full_v2$qca.highcourt <- round(Data_QCA_full_v2$qca.highcourt, 3)

Data_QCA_full_v2$qca.soccomlegit <- calibrate(Data_QCA_full_v2$vdem_20ya_legideo_1, type = "fuzzy", method = "direct", c(0.05, 0.201, 0.65), logistic = TRUE)
Data_QCA_full_v2$qca.soccomlegit <- round(Data_QCA_full_v2$qca.soccomlegit, 3)

Data_QCA_full_v2$qca.rellegit <- calibrate(Data_QCA_full_v2$vdem_20ya_legideo_4, type = "fuzzy", method = "direct", c(0.05, 0.201, 0.65), logistic = TRUE)
Data_QCA_full_v2$qca.rellegit <- round(Data_QCA_full_v2$qca.rellegit, 3)





### Outcome QCA 1

Data_QCA_full$qca.support.rel <- calibrate(Data_QCA_full$LXX2014X, type = "fuzzy", method = "direct", c(6, 10.9, 25), logistic = TRUE)
Data_QCA_full$qca.support.rel <- round(Data_QCA_full$qca.support.rel, 3)

### Modified QCA Outcome 1

Data_QCA_full$qca.support.rel2 <- calibrate(Data_QCA_full$LXX2014X, type = "fuzzy", method = "direct", c(6, 8.1, 20), logistic = TRUE)
Data_QCA_full$qca.support.rel2 <- round(Data_QCA_full$qca.support.rel2, 3)

### Calibration Plots

rel.CAL <- plot(Data_QCA_full$al_religion2000, Data_QCA_full$qca.relfrag, xlab = "Religious fractionalization (raw)",
             ylab = "Religious fractionalization (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

gdp.CAL <- plot(Data_QCA_full$mad_gdppc, Data_QCA_full$qca.gdp, xlab = "GDP per capita (raw)",
                ylab = "GDP per capita (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 12500, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

fog.CAL <- plot(Data_QCA_full$fh_fog, Data_QCA_full$qca.fog, xlab = "Functioning of Government (raw)",
                ylab = "Functioning of Government (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 7, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

supp.relgrp.CAL <- plot(Data_QCA_full$vdem_20ya_regsup_g7, Data_QCA_full$qca.supp.relgrp, xlab = "Support from religious groups (20 year average, raw)",
                        ylab = "Support from religious groups (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.35, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

supp.urbanwk.CAL <- plot(Data_QCA_full$vdem_20ya_regsup_g9, Data_QCA_full$qca.supp.urbanwk, xlab = "Support from urban working class and unions (20 year average, raw)",
                        ylab = "Support from urban working class and unions (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.35, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)


## QCA2

highcourt.CAL <- plot(Data_QCA_full_v2$vdem_20ya_juhcind, Data_QCA_full_v2$qca.highcourt, xlab = "High court independence (20 year average, raw)",
                        ylab = "High court independence (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

ideo.soccom.CAL <- plot(Data_QCA_full_v2$vdem_20ya_legideo_1, Data_QCA_full_v2$qca.soccomlegit, xlab = "soc/communist legitimacy claims (20 year average, raw)",
                        ylab = "soc/communist legitimacy claims  (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.201, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

ideo.rel.CAL <- plot(Data_QCA_full_v2$vdem_20ya_legideo_4, Data_QCA_full_v2$qca.rellegit, xlab = "religious legitimacy claims (20 year average, raw)",
                        ylab = "religious legitimacy claims  (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.201, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)





## outcome

supp.rel.CAL <- plot(Data_QCA_full$LXX2014X, Data_QCA_full$qca.support.rel, xlab = "Religious regulation - support (raw)",
                         ylab = "Religious regulation - support (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 10.9, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)



## Alternative calibrations by Elina 

# Fractionalization 

# Calibration 
Data_QCA_full$qca.relfrag_ES <- calibrate(Data_QCA_full$al_religion2000, type = "fuzzy", method = "direct", c(0.2, 0.5, 0.75), logistic = TRUE)

# Plot 
rel.CAL_ES <- plot(Data_QCA_full$al_religion2000, Data_QCA_full$qca.relfrag_ES, xlab = "Religious fractionalization (raw)",
                ylab = "Religious fractionalization (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

# Check that there are no values which are both in and out of the set (0.5)
Forbidden1 <- as.numeric(Data_QCA_full$qca.relfrag_ES == 0.5)
sum(Forbidden1, na.rm = TRUE)

# Check for skewness: Proportion of cases in and out of the set 
Skewness1 <-as.numeric(Data_QCA_full$qca.relfrag_ES > 0.5)
sum(Skewness1, na.rm = TRUE)
prop.table(table(Skewness1))

## GDP/capita 

Data_QCA_full$qca.gdp_ES <- calibrate(Data_QCA_full$mad_gdppc, type = "fuzzy", method = "direct", c(2000, 12500, 40000), logistic = TRUE)

Forbidden2 <- as.numeric(Data_QCA_full$qca.gdp_ES == 0.5)
sum(Forbidden2, na.rm = TRUE)

Skewness2 <-as.numeric(Data_QCA_full$qca.gdp_ES > 0.5)
sum(Skewness2, na.rm = TRUE)
prop.table(table(Skewness2))

gdp.CAL_ES <- plot(Data_QCA_full$mad_gdppc, Data_QCA_full$qca.gdp_ES, xlab = "GDP per capita (raw)",
                ylab = "GDP per capita (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 12500, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

## Support from religious groups 

Data_QCA_full$qca.supp.relgrp_ES <- calibrate(Data_QCA_full$vdem_20ya_regsup_g7, type = "fuzzy", method = "direct", c(0.05, 0.10, 0.46), logistic = TRUE)

Forbidden3 <- as.numeric(Data_QCA_full$qca.supp.relgrp_ES == 0.5)
sum(Forbidden3, na.rm = TRUE)

Skewness3 <-as.numeric(Data_QCA_full$qca.supp.relgrp_ES > 0.5)
sum(Skewness3, na.rm = TRUE)
prop.table(table(Skewness3))

Skewness3b <-as.numeric(Data_QCA_full$qca.supp.relgrp > 0.5)
prop.table(table(Skewness3b))



supp.relgrp.CAL_ES <- plot(Data_QCA_full$vdem_20ya_regsup_g7, Data_QCA_full$qca.supp.relgrp_ES, xlab = "Support from religious groups (20 year average, raw)",
                        ylab = "Support from religious groups (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.10, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)



## Support from urban left 

Data_QCA_full$qca.supp.urbanwk_ES <- calibrate(Data_QCA_full$vdem_20ya_regsup_g9, type = "fuzzy", method = "direct", c(0.05, 0.10, 0.46), logistic = TRUE)

Forbidden4 <- as.numeric(Data_QCA_full$qca.supp.urbanwk_ES == 0.5)
sum(Forbidden4, na.rm = TRUE)

Skewness4 <-as.numeric(Data_QCA_full$qca.supp.urbanwk_ES > 0.5)
sum(Skewness4, na.rm = TRUE)
prop.table(table(Skewness4))

Skewness4b <-as.numeric(Data_QCA_full$qca.supp.urbanwk > 0.5)
prop.table(table(Skewness4b))

supp.urbanwk.CAL_ES <- plot(Data_QCA_full$vdem_20ya_regsup_g9, Data_QCA_full$qca.supp.urbanwk_ES, xlab = "Support from urban working class and unions (20 year average, raw)",
                         ylab = "Support from urban working class and unions (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.10, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

### Outcome QCA 1: Religious support 

Data_QCA_full$qca.support.rel_ES <- calibrate(Data_QCA_full$LXX2014X, type = "fuzzy", method = "direct", c(6, 11, 25), logistic = TRUE)

Forbidden5 <- as.numeric(Data_QCA_full$qca.support.rel_ES == 0.5)
sum(Forbidden5, na.rm = TRUE)

Skewness5 <-as.numeric(Data_QCA_full$qca.support.rel_ES > 0.5)
sum(Skewness5, na.rm = TRUE)
prop.table(table(Skewness5))

Skewness6 <- as.numeric(Data_QCA_full$qca.support.rel > 0.5)
prop.table(table(Skewness5))


supp.rel.CAL_ES <- plot(Data_QCA_full$LXX2014X, Data_QCA_full$qca.support.rel_ES, xlab = "Religious regulation - support (raw)",
                     ylab = "Religious regulation - support (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 10, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)



## write csv
setwd("C:/Users/fe300/Desktop/Research Article - Reg Rel QCA")
setwd("E:/Fortis/Workspace/Research Article - QCA Religious Regulation")

write.csv(Data_QCA_full, "Data_QCA_full.csv")


### accept Elina's changes and save to new file

Data_QCA_full_fin <- Data_QCA_full
Data_QCA_fin_v2 <- Data_QCA_full_v2


Data_QCA_full_fin$qca.relfrag <- Data_QCA_full_fin$qca.relfrag_ES
Data_QCA_full_fin$qca.gdp <- Data_QCA_full_fin$qca.gdp_ES
Data_QCA_full_fin$qca.supp.relgrp <- Data_QCA_full_fin$qca.supp.relgrp_ES
Data_QCA_full_fin$qca.supp.urbanwk <- Data_QCA_full_fin$qca.supp.urbanwk_ES
Data_QCA_full_fin$qca.support.rel <- Data_QCA_full_fin$qca.support.rel_ES

Data_QCA_full_fin$qca.relfrag_ES <- NULL
Data_QCA_full_fin$qca.gdp_ES <- NULL
Data_QCA_full_fin$qca.supp.relgrp_ES <- NULL
Data_QCA_full_fin$qca.supp.urbanwk_ES <- NULL
Data_QCA_full_fin$qca.support.rel_ES <- NULL

Data_QCA_full_fin$qca.relfrag <- round(Data_QCA_full_fin$qca.relfrag, 2)
Data_QCA_full_fin$qca.gdp <- round(Data_QCA_full_fin$qca.gdp, 2)
Data_QCA_full_fin$qca.supp.relgrp <- round(Data_QCA_full_fin$qca.supp.relgrp, 2)
Data_QCA_full_fin$qca.supp.urbanwk <- round(Data_QCA_full_fin$qca.supp.urbanwk, 2)
Data_QCA_full_fin$qca.support.rel <- round(Data_QCA_full_fin$qca.support.rel, 2)

na.omit(Data_QCA_full_fin) -> Data_QCA_full_fin
na.omit(Data_QCA_fin_v2) -> Data_QCA_fin_v2

write.csv(VDEM12.IP2, "20_year_vdem_average_data.csv")
write.csv(Data_QCA_full_fin, "Data_QCA_full_final_outcome_mod.csv")

write.csv(Data_QCA_fin_v2, "Data_QCA2.csv")

